using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using CustomUIControls;

namespace Checkr
{
    public class SkinManager
    {
        private string directory = Form1.BASE_DIR + "\\skins";
        public string currentSkin;
        private TaskbarNotifier taskBar;
        public Font titleFont = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
        public Font contentFont = new Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Pixel);
        public Color titleColor = Color.FromArgb(0, 0, 0);
        public Color contentColor = Color.FromArgb(0, 0, 0);
        public Color hoverColor = Color.FromArgb(0, 0, 255);

        public SkinManager(TaskbarNotifier tskBar)
        {
            taskBar = tskBar;
            currentSkin = directory + "\\default\\";

            //Based on 280x100 image
            taskBar.SetBackgroundBitmap(currentSkin + "skin.bmp", Color.FromArgb(255, 0, 255));
            taskBar.SetCloseBitmap(currentSkin + "close.bmp", Color.FromArgb(255, 0, 255), new Point(260, 5));
            taskBar.TitleRectangle = new Rectangle(10, 0, 200, 25);
            taskBar.ContentRectangle = new Rectangle(15, 35, 260, 53);
            taskBar.NormalTitleFont = titleFont;
            taskBar.NormalContentFont = contentFont;
            taskBar.HoverContentFont = contentFont;//same
            taskBar.NormalTitleColor = titleColor;
            taskBar.NormalContentColor = contentColor;
            taskBar.HoverContentColor = hoverColor;
        }

        public void updateColoursFonts()
        {
            taskBar.NormalTitleFont = titleFont;
            taskBar.NormalContentFont = contentFont;
            taskBar.HoverContentFont = contentFont;//same
            taskBar.NormalTitleColor = titleColor;
            taskBar.NormalContentColor = contentColor;
            taskBar.HoverContentColor = hoverColor;
        }

        /*
         * Setting of fonts still to be implemeneted
         */ 
        public void loadSkin(string skin)
        {
            currentSkin = skin;
            taskBar.SetBackgroundBitmap(currentSkin + "skin.bmp", Color.FromArgb(255, 0, 255));
            taskBar.SetCloseBitmap(currentSkin + "close.bmp", Color.FromArgb(0, 0, 255), new Point(250, 5));

            try
            {
                Console.WriteLine("Reading skin file: " + skin);
                StreamReader read = File.OpenText(skin + "conf.cfg");

                //Title Font
                string[] temp = read.ReadLine().Split(':');

                /*float size = (float)Convert.ToSingle(temp[1]);
                size = (float)Math.Round(size);

                FontStyle tempStyle = new FontStyle();
                if (temp[2] == "Bold") tempStyle = FontStyle.Bold;
                else if (temp[2] == "Regular") tempStyle = FontStyle.Regular;
                else if (temp[2] == "Italic") tempStyle = FontStyle.Italic;
                else if (temp[2] == "Strikeout") tempStyle = FontStyle.Strikeout;
                else if (temp[2] == "Underline") tempStyle = FontStyle.Underline;
                else tempStyle = FontStyle.Regular;

                Console.WriteLine(temp[0] + ":" + temp[1] + ":" + temp[2]);
                titleFont = new Font(temp[0], size, tempStyle, GraphicsUnit.Pixel);*/

                temp = read.ReadLine().Split(':');
                Console.WriteLine(temp[0] + ":" + temp[1] + ":" + temp[2]);
                titleColor = Color.FromArgb(Convert.ToInt16(temp[0]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[2]));

                temp = read.ReadLine().Split(':');
                /*if (temp[2] == "Bold") tempStyle = FontStyle.Bold;
                else if (temp[2] == "Regular") tempStyle = FontStyle.Regular;
                else if (temp[2] == "Italic") tempStyle = FontStyle.Italic;
                else if (temp[2] == "Strikeout") tempStyle = FontStyle.Strikeout;
                else if (temp[2] == "Underline") tempStyle = FontStyle.Underline;
                else tempStyle = FontStyle.Regular;
                Console.WriteLine(temp[0] + ":" + temp[1] + ":" + temp[2]);
                contentFont = new Font(temp[0], Convert.ToInt16(temp[1]), tempStyle, GraphicsUnit.Pixel);*/

                temp = read.ReadLine().Split(':');
                Console.WriteLine(temp[0] + ":" + temp[1] + ":" + temp[2]);
                contentColor = Color.FromArgb(Convert.ToInt16(temp[0]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[2]));

                temp = read.ReadLine().Split(':');
                Console.WriteLine(temp[0] + ":" + temp[1] + ":" + temp[2]);
                hoverColor = Color.FromArgb(Convert.ToInt16(temp[0]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[2]));
                read.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            updateColoursFonts();
        }

        public void setSkin(string skin)
        {
            currentSkin = skin;
            taskBar.SetBackgroundBitmap(currentSkin + "skin.bmp", Color.FromArgb(255, 0, 255));
            taskBar.SetCloseBitmap(currentSkin + "close.bmp", Color.FromArgb(0, 0, 255), new Point(250, 5));

            try
            {
                StreamWriter writer = new StreamWriter(skin + "conf.cfg");
                writer.WriteLine(titleFont.Name.ToString() + ":" + titleFont.Size.ToString() + ":" + titleFont.Style.ToString());
                writer.WriteLine(titleColor.R.ToString() + ":" + titleColor.G.ToString() + ":" + titleColor.B.ToString());
                writer.WriteLine(contentFont.Name.ToString() + ":" + contentFont.Size.ToString() + ":" + contentFont.Style.ToString());
                writer.WriteLine(contentColor.R.ToString() + ":" + contentColor.G.ToString() + ":" + contentColor.B.ToString());
                writer.WriteLine(hoverColor.R.ToString() + ":" + hoverColor.G.ToString() + ":" + hoverColor.B.ToString());
                writer.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public ArrayList loadSkins()
        {
            ArrayList skins = new ArrayList();
            try
            {
                if (Directory.Exists(directory))
                {
                    foreach (string dirTemp in Directory.GetDirectories(directory))
                    {
                        if (File.Exists(dirTemp + "/skin.bmp") && File.Exists(dirTemp + "/close.bmp"))
                        {
                            skins.Add(dirTemp);
                        }   
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return skins;
        }
    }
}
